import { setToken, removeToken, getToken } from '@/utils/auth'
import { getInfo, getInfobyid } from '@/api/user'
export default {
  namespaced: true,
  state: {
    // getToekn() 优先从cookie 中获取token
    token: getToken() || '',
    UserInfo: {}
  },
  mutations: {
    // 更新存储token
    updataToken(state, token) {
      state.token = token
      // 将token存储到cookie中实现持久化
      setToken(token)
    },
    // 清空token
    delToken(state) {
      state.token = ''
      // 清空cookie中的token
      removeToken()
    },
    // 更新用户信息
    updataUserInfo(state, info) {
      state.UserInfo = info
    },
    delupdateUserInfo(state) {
      state.UserInfo = {}
    }
  },
  actions: {
    async getuserinfo(content) {
      // conetnt store 仓库实例
      try {
        const res = await getInfo()
        console.log(res, 'res')
        const info = await getInfobyid(res.data.userId)
        console.log(info, 'info.data')
        content.commit('updataUserInfo', { ...res.data, ...info.data })
        return res.data.roles.menus
      } catch (err) {
        console.log(err)
      }
    }
  }

}
